package com.interesting.client.UI.video;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.actionbarsherlock.app.ActionBar;
import com.interesting.R;
import com.interesting.base.core.BaseActivity;

public class VideoAndMusicActivity extends BaseActivity implements OnCheckedChangeListener {
	private Fragment fragment;
	private FragmentTransaction ft;
	private RadioGroup radioBar;
	private String[] tag = { "music", "video" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.video_activity_frame);

		setActionBar();

		ft = getSupportFragmentManager().beginTransaction();
		if (savedInstanceState != null) {
			fragment = getSupportFragmentManager().getFragment(savedInstanceState, tag[0]);
		}
		if (fragment == null) {
			fragment = new MusicFragment();
		}
		ft.replace(R.id.video_activity_frame, fragment, tag[0]);
		ft.commit();
	}

	private void setActionBar() {
		ActionBar bar = getSupportActionBar();
		bar.setCustomView(R.layout.two_radio_actionbar_custom_view);
		radioBar = (RadioGroup) bar.getCustomView().findViewById(R.id.hot_pic_fragment_radiogroup_two);
		RadioButton first = (RadioButton) bar.getCustomView().findViewById(R.id.two_radio_bar_first);
		RadioButton second = (RadioButton) bar.getCustomView().findViewById(R.id.two_radio_bar_second);
		first.setText("音乐");
		second.setText("视频");
		radioBar.setOnCheckedChangeListener(this);
	}

	public void ToActivity(Intent intent) {
		startActivity(intent);
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		Fragment fragment = null;
		String type = null;
		switch (checkedId) {
		case R.id.two_radio_bar_first:
			type = tag[0];
			fragment = getSupportFragmentManager().findFragmentByTag(type);
			if (fragment == null)
				fragment = new MusicFragment();
			break;
		case R.id.two_radio_bar_second:
			type = tag[1];
			fragment = getSupportFragmentManager().findFragmentByTag(type);
			if (fragment == null)
				fragment = new VideoFragment();
			break;
		}
		getSupportFragmentManager().beginTransaction().replace(R.id.video_activity_frame, fragment, type)
				.addToBackStack(type).commit();
	}
}
